package xin.clips.system.service.impl;

import java.util.List;

import xin.clips.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.clips.common.core.text.Convert;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.system.domain.gy.GyUserDoc;
import xin.clips.system.domain.gy.GyUserShareDoc;
import xin.clips.system.mapper.gy.GyUserDocMapper;
import xin.clips.system.mapper.gy.GyUserShareDocMapper;
import xin.clips.system.service.gy.IGyUserShareDocService;

/**
 * 用户分享的文档Service业务层处理
 *
 * @author clips
 * @date 2023-03-07
 */
@Service
public class GyUserShareDocServiceImpl implements IGyUserShareDocService {
    @Autowired
    private GyUserShareDocMapper gyUserShareDocMapper;

    @Autowired
    private GyUserDocMapper gyUserDocMapper;


    /**
     * 查询用户分享的文档
     *
     * @param shareId 用户分享的文档主键
     * @return 用户分享的文档
     */
    @Override
    public GyUserShareDoc selectGyUserShareDocByShareId(Long shareId) {
        return gyUserShareDocMapper.selectGyUserShareDocByShareId(shareId);
    }

    /**
     * 查询用户分享的文档列表
     *
     * @param gyUserShareDoc 用户分享的文档
     * @return 用户分享的文档
     */
    @Override
    public List<GyUserShareDoc> selectGyUserShareDocList(GyUserShareDoc gyUserShareDoc) {
        return gyUserShareDocMapper.selectGyUserShareDocList(gyUserShareDoc);
    }

    /**
     * 新增用户分享的文档
     *
     * @param gyUserShareDoc 用户分享的文档
     * @return 结果
     */
    @Override
    public int insertGyUserShareDoc(GyUserShareDoc gyUserShareDoc) {

        GyUserShareDoc shareDoc = gyUserShareDocMapper.selectGyUserShareDocByFileId(gyUserShareDoc.getFileId());
        if (shareDoc == null) {
            GyUserDoc userDoc = gyUserDocMapper.selectGyUserDocByFileId(gyUserShareDoc.getFileId());
            gyUserShareDoc.setFileName(userDoc.getFileName());
            gyUserShareDoc.setFileExt(userDoc.getFileExt());
            gyUserShareDoc.setRecviveStatus(1);
            gyUserShareDoc.setValidTime(DateUtils.getNowDate());
            gyUserShareDoc.setCreateTime(DateUtils.getNowDate());
            gyUserShareDoc.setUserName(ShiroUtils.getLoginName());
            gyUserShareDoc.setCreateBy(ShiroUtils.getLoginName());
            gyUserShareDoc.setUserId(ShiroUtils.getUserId());
            gyUserShareDoc.setRecviveStatus(1);
            gyUserShareDoc.setUrlInfo(userDoc.getFilePath());
            return gyUserShareDocMapper.insertGyUserShareDoc(gyUserShareDoc);
        } else {
            shareDoc.setRecviveStatus(1);
            shareDoc.setValidTime(DateUtils.getNowDate());
            shareDoc.setValidState(gyUserShareDoc.getValidState());
            shareDoc.setUserName(ShiroUtils.getLoginName());
            shareDoc.setPwd(gyUserShareDoc.getPwd());
            shareDoc.setUpdateTime(DateUtils.getNowDate());
            shareDoc.setUserId(ShiroUtils.getUserId());
            return gyUserShareDocMapper.updateGyUserShareDoc(shareDoc);
        }
    }

    /**
     * 修改用户分享的文档
     *
     * @param gyUserShareDoc 用户分享的文档
     * @return 结果
     */
    @Override
    public int updateGyUserShareDoc(GyUserShareDoc gyUserShareDoc) {
        gyUserShareDoc.setUpdateTime(DateUtils.getNowDate());
        return gyUserShareDocMapper.updateGyUserShareDoc(gyUserShareDoc);
    }

    /**
     * 批量删除用户分享的文档
     *
     * @param shareIds 需要删除的用户分享的文档主键
     * @return 结果
     */
    @Override
    public int deleteGyUserShareDocByShareIds(String shareIds) {
        return gyUserShareDocMapper.deleteGyUserShareDocByShareIds(Convert.toStrArray(shareIds));
    }

    /**
     * 删除用户分享的文档信息
     *
     * @param shareId 用户分享的文档主键
     * @return 结果
     */
    @Override
    public int deleteGyUserShareDocByShareId(Long shareId) {
        return gyUserShareDocMapper.deleteGyUserShareDocByShareId(shareId);
    }

    /**
     * 查询用户分享的文档
     *
     * @param fileId 用户分享的文件id
     * @return 用户分享的文档
     */
    public GyUserShareDoc selectGyUserShareDocByFIleId(String fileId){
        return gyUserShareDocMapper.selectGyUserShareDocByFIleId(fileId);
    }
}
